﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Threading;

namespace Server
{
  class TServer
  {
    static void Main(string[] args)
    {
      if (!HttpListener.IsSupported)
      {
        Console.WriteLine("Can't run http server on your platform");
        return;
      }

      const int DefaultPortNumber = 80;
      int Port = args.Count() > 0 ? int.Parse(args[0]) : DefaultPortNumber;

      try
      {
        using (HttpListener server = new HttpListener())
        {
          server.Prefixes.Add("http://*:" + Port.ToString() + "/");
          server.Start();

          Console.WriteLine("Http server start on {0} port.", Port);

          while (true) 
            TRequestProcessor.CreateProcessor(server.GetContext());
        }
      }
      catch (Exception e)
      {
        Console.WriteLine("Exception: {0}\n\nPress any key...", e.Message);
        Console.ReadKey();
      }
    }
  }
}
